<?php
/**
 * Created by PhpStorm.
 * User: f
 * Date: 2017/12/27
 * Time: 10:47
 */

namespace App\Http\Controllers;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

class ApiController extends Controller
{
    protected $url='39.104.77.47:20621';
    protected $user='user';
    protected $password='password';
    /*
     * 请求钱包方法
     * $method  方法名
     * $params array  参数
     * */
    private function zzmRequest($method,$params)
    {
        $client = new Client();
        try{
            $res = $client->request('POST',$this->url,[
                'auth'=>[$this->user,$this->password],
                'json'=>['method'=>$method,'params'=>$params]
            ]);
            return json_decode($res->getBody()->getContents(),true);
        }catch (\Exception $exception){
            \Log::info($exception->getMessage());
            return ['result'=>'','error'=>'钱包操作失败！'];
        }
    }
    /*
     * 获取新地址
     * $account 账户名  手机或邮箱 或其他
     *
     * */
    public function getNewAddress(Request $request,$account)
    {
        $response = $this->zzmRequest('getnewaddress',[$account]);
        if($response['error']==null){
            return response()->json(['code'=>1,'address'=>$response['result'],'msg'=>'生成地址成功！']);
        }
        return response()->json(['code'=>0,'address'=>'','msg'=>'生成地址失败！']);
    }

    /*
     * 查询账户转入记录
     * */
    public function query(Request $request,$account)
    {
        $response = $this->zzmRequest('listtransactions',[$account]);
        if(count($response['result'])){
            foreach ($response['result'] as $k=>$transfer){
                $data[$k]['account'] = $transfer['account'];
                $data[$k]['address'] = $transfer['address'];
                $data[$k]['category'] = $transfer['category'];
                $data[$k]['amount'] = $transfer['amount'];
                $data[$k]['confirmations'] = $transfer['confirmations'];
                $data[$k]['txid'] = $transfer['txid'];
                $data[$k]['timereceived'] = $transfer['timereceived'];
            }
            return response()->json(['code'=>1,'data'=>$data,'msg'=>'']);
        }
        return response()->json(['code'=>0,'data'=>'','msg'=>'该账户还没有转入记录！']);
    }

    /*
     * 转出
     * */
    public function transfer_out(Request $request)
    {
        $receiveAddress = $request->input('address');//接收地址
        $amount = $request->input('amount');//转出数量
        $this->walletlock();//加锁
        $this->walletpassphrase();//解锁钱包
        $response = $this->zzmRequest('sendtoaddress',[$receiveAddress,round($amount,8)]);
        if($response['error']==null){
            return response()->json(['code'=>1,'txid'=>$response['result'],'msg'=>'转出成功！']);
        }
        return response()->json(['code'=>0,'txid'=>'','msg'=>'转出失败！']);
    }


    /*
     *所有账户的交易记录
     * */
    public function transferOutByAccount($address)
    {
        $record = $this->zzmRequest('listtransactions',[]);
        $data = [];
        foreach($record['result'] as $key=>$value){
            if($value['address']==$address){
                $data[$key]['address'] = $value['address'];
                $data[$key]['category'] = $value['category'];
                $data[$key]['amount'] = $value['amount'];
                $data[$key]['confirmations'] =$value['confirmations'];
                $data[$key]['blockhash'] = $value['blockhash'];
                $data[$key]['txid'] = $value['txid'];
                $data[$key]['timereceived'] = $value['timereceived'];
            }
        }
        return response()->json(['code'=>1,'data'=>array_values($data),'msg'=>'']);
    }

    /*
     * 获取服务器钱包总余额
     * */
    public function getbalance()
    {
        $balance = $this->zzmRequest('getbalance',[]);
        return response()->json(['code'=>1,'balance'=>$balance['result'],'msg'=>'']);
    }

    /*
     *解锁钱包
     * */
    public  function walletpassphrase()
    {
        $client = new Client();
        $client->request('POST',$this->url,[
            'auth'=>[$this->user,$this->password],
            'json'=>['method'=>'walletpassphrase','params'=>['root',10]]
        ]);
    }

    /*
     * 加锁
     * */
    public function walletlock()
    {
        $client = new Client();
        $client->request('POST',$this->url,[
            'auth'=>[$this->user,$this->password],
            'json'=>['method'=>'walletlock','params'=>[]]
        ]);
    }
}